package com.geeksaga.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.geeksaga.domain.BbsType;
import com.geeksaga.domain.SagaCategory;
import com.geeksaga.service.BbsTypeService;
import com.geeksaga.service.CategoryService;

/**
 * @author newbie
 * @since 0.1
 */
@Controller
public class CategoryController {

    protected static final Log logger = LogFactory.getLog(CategoryController.class);
    
    @Autowired private CategoryService bbsCategoryService = null;
    @Autowired private BbsTypeService bbsTypeService = null;
    
    /**
     * @return
     */
    public String submit() {
        bbsCategoryService.add(new SagaCategory());
        
        return "";
    }
    
    /**
     * @param model
     * @param request
     * @return
     */
    @RequestMapping( value = { "/category/list.do" } )
    public String list(ModelMap model, HttpServletRequest request) {
        
        List<SagaCategory> list = bbsCategoryService.list();
        
        model.addAttribute("list", list);
        
        return "category/list";
    }
    
    /**
     * @param model
     * @param request
     * @return
     */
    @RequestMapping( value = { "/category/edit.do" } )
    public String edit(ModelMap model, HttpServletRequest request) {
        
//        model.addAttribute("targetId", codeService.findCacheByPrimaryCode("A008"));
        
        List<BbsType> bbsTypeList = bbsTypeService.list();
        
        Map<String, String> targetId = new HashMap<String, String>();
        
        for(BbsType bbsType : bbsTypeList)
            targetId.put(bbsType.getBbsId(), bbsType.getBbsName());
        
        model.addAttribute("targetId", targetId);
        model.addAttribute("category", new SagaCategory());
        
        return "category/edit";
    }
}